package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.Threads;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.crm.entity.CrmCommunicateHistory;
import com.yuyou.fn.crm.entity.CrmStudentCommunicate;
import com.yuyou.fn.crm.service.ICrmCommunicateHistoryService;
import com.yuyou.fn.crm.service.ICrmStudentCommunicateService;
import com.yuyou.fn.educational.entity.ClassLecture;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.IClassLectureService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IBusTeamService;
import com.yuyou.fn.platform.service.IOauthService;
import com.yuyou.fn.portal.service.IOuterCourseClassService;
import com.yuyou.fn.report.entity.ClassLectureDetail;
import com.yuyou.fn.report.service.IClassLectureDetailService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

@EventHandler(tables = {"yyfn.teacher"})
@Component
public class TeacherChangeUpdater implements IEventHandler {

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IClassLectureService classLectureService;

    @Resource
    private IOuterCourseClassService outerCourseClassService;

    @Resource
    private IClassLectureDetailService classLectureDetailService;

    @Resource
    private IOauthService oauthService;

    @Resource
    private ICrmStudentCommunicateService crmStudentCommunicateService;

    @Resource
    private ICrmCommunicateHistoryService crmCommunicateHistoryService;

    @Resource
    private IBusTeamService busTeamService;

    @Override
    public void insert(String tableName, JSONArray rowList) {

    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        if (updateColumns.contains("status")){/*不在职的老师删除微信绑定*/
            for (int i = 0; i < rowList.size(); i++){
                JSONObject row = rowList.getJSONObject(i);
                Teacher teacher = row.toJavaObject(Teacher.class);
                if (teacher.getStatus()!=1){
                    oauthService.deleteByUserIdRole(teacher.getTeacherId(),2);
                }
            }
        }

        if (updateColumns.contains("job_nature")){
            for (int i = 0; i < rowList.size(); i++){
                JSONObject row = rowList.getJSONObject(i);
                Teacher teacher = row.toJavaObject(Teacher.class);
                Date date=TimeUtils.nowDate();
                Example<ClassLectureDetail> example= ClassLectureDetail.createExample();
                example.createCriteria().andEqualTo("teacherId",teacher.getTeacherId()).andGreaterThanOrEqualTo("classDate",date);
                ClassLectureDetail update=new ClassLectureDetail();
                update.setJobNature(teacher.getJobNature());
                classLectureDetailService.updateSelectiveByExample(update,example);

                Example<CourseClass> courseClassExample=CourseClass.createExample();
                courseClassExample.createCriteria().andEqualTo("teacherId",teacher.getTeacherId());
                CourseClass courseClass=new CourseClass();
                courseClass.setJobNature(teacher.getJobNature());
                courseClass.setUpdateTime(TimeUtils.now());
                courseClassService.updateSelectiveByExample(courseClass,courseClassExample);
            }
        }

        if (updateColumns.contains("bus_team_id")){
            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                Teacher teacher = row.toJavaObject(Teacher.class);
                if (teacher.getBusTeamId()!=null){
                    BusTeam busTeam=busTeamService.findById(teacher.getBusTeamId());
                    Example<CrmStudentCommunicate> crmStudentCommunicateExample=CrmStudentCommunicate.createExample();
                    crmStudentCommunicateExample.createCriteria().andEqualTo("seniorId",teacher.getTeacherId());
                    CrmStudentCommunicate crmStudentCommunicate=new CrmStudentCommunicate();
                    crmStudentCommunicate.setSeniorBusTeamName(busTeam.getName());
                    crmStudentCommunicate.setSeniorBusTeamId(busTeam.getBusTeamId());
                    crmStudentCommunicateService.updateSelectiveByExample(crmStudentCommunicate,crmStudentCommunicateExample);

                }

            }


        }

        if (updateColumns.contains("teacher_name")) {

            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                Teacher teacher = row.toJavaObject(Teacher.class);
                Example<StudentReg> studentRegExample = StudentReg.createExample();
                studentRegExample.createCriteria().andEqualTo("courseOrderId", teacher.getTeacherId());

                StudentReg updateStudent = new StudentReg();
                updateStudent.setCounselorName(teacher.getTeacherName());
                updateStudent.setUpdateTime(TimeUtils.now());
                studentRegService.updateSelectiveByExample(updateStudent, studentRegExample);

                Example<CourseClass> courseClassExample = CourseClass.createExample();
                courseClassExample.selectProperties("classId");
                courseClassExample.createCriteria().andLike("teacherIds" ,"%"+teacher.getTeacherId()+"%");
                List<CourseClass> courseClassList = courseClassService.findListByExample(courseClassExample);
                for(CourseClass courseClass : courseClassList ){
                    Threads.dispatch(()-> courseClassService.updateTeacherInfo(courseClass.getClassId()));
                }

                Example<ClassLecture> classLectureExample=ClassLecture.createExample();
                classLectureExample.createCriteria().andEqualTo("teacherId",teacher.getTeacherId());
                ClassLecture classLecture=new ClassLecture();
                classLecture.setTeacherName(teacher.getTeacherName());
                classLectureService.updateSelectiveByExample(classLecture,classLectureExample);


                Example<CrmStudentCommunicate> crmStudentCommunicateExample=CrmStudentCommunicate.createExample();
                crmStudentCommunicateExample.createCriteria().andEqualTo("seniorId",teacher.getTeacherId());
                CrmStudentCommunicate crmStudentCommunicate=new CrmStudentCommunicate();
                crmStudentCommunicate.setSeniorName(teacher.getTeacherName());
                crmStudentCommunicateService.updateSelectiveByExample(crmStudentCommunicate,crmStudentCommunicateExample);

                Example<CrmCommunicateHistory> crmCommunicateHistoryExample=CrmCommunicateHistory.createExample();
                crmCommunicateHistoryExample.createCriteria().andEqualTo("seniorId",teacher.getTeacherId());
                CrmCommunicateHistory crmCommunicateHistory=new CrmCommunicateHistory();
                crmCommunicateHistory.setSeniorName(teacher.getTeacherName());
                crmCommunicateHistoryService.updateSelectiveByExample(crmCommunicateHistory,crmCommunicateHistoryExample);


            }
        }

        if (updateColumns.contains("avatar_url") ) {
            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                Teacher teacher = row.toJavaObject(Teacher.class);

                //updateEvent -> CourseClass -> OuterCourseClass
                Example<CourseClass> courseClassExample = CourseClass.createExample();
                courseClassExample.createCriteria().andLike("teacherIds" ,"%"+teacher.getTeacherId()+"%");
                CourseClass updateCourseClass = new CourseClass();
                updateCourseClass.setUpdateTime(TimeUtils.now());
                courseClassService.updateSelectiveByExample(updateCourseClass,courseClassExample);
            }
        }
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }
}
